package com.jlu155.CourseManager.service;

import com.jlu155.CourseManager.Bean.PageRequest;
import com.jlu155.CourseManager.Bean.PageResult;
import com.jlu155.CourseManager.entity.Course;
import com.jlu155.CourseManager.entity.TeachClass;
import com.jlu155.CourseManager.repository.CollegeRepository;
import com.jlu155.CourseManager.repository.CourseRepository;
import com.jlu155.CourseManager.repository.TeachClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private TeachClassRepository teachClassRepository;
    @Autowired
    private CollegeRepository collegeRepository;
    @Autowired
    private TeachClassService teachClassService;
    public void fillViewInfo(Course course,boolean withTeachCourses,boolean withSessions,boolean withStudents){
        if(withTeachCourses){
            course.setTeachClassList(teachClassRepository.findTeachClassByCourseId(course.getCourse_id()));
            for(TeachClass tc:course.getTeachClassList()){
                teachClassService.associateMult(tc,withSessions,withStudents);
            }
        }
    }
    public PageResult<Course> getCoursePage(PageRequest pageRequest){
        PageResult<Course> pageResult=new PageResult<Course>();
        List<Course> courses;
        courses=courseRepository.findCourseByCond(pageRequest.getName(),pageRequest.getCourse_id(),pageRequest.getCollege_id(),(pageRequest.getIndex()-1)* pageRequest.getSize(),pageRequest.getSize());
        for(Course c:courses){
            fillViewInfo(c,true,false,false);
        }

        int total= courseRepository.findCourseByCond(pageRequest.getName(),pageRequest.getCourse_id(),pageRequest.getCollege_id(),0,1000000).size();
        pageResult.setPageList(courses);
        pageResult.setPageNumber(total);
        return pageResult;
    }
}
